/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package dk.redillusions.jdbc.proxy.statistics;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;

import dk.redillusions.jdbc.proxy.ConnectionAdapterDelegate;

/**
 * Collects statistics for actions performed on a {@link java.sql.Connection}.
 * 
 * @author Sune Wettersteen
 * 
 */
final class ConnectionStatisticsDelegate extends ConnectionAdapterDelegate {
    /**
     * Contains statistics about the {@link java.sql.Connection}.
     */
    private final IConnectionStatisticsCollector connectionStatistics;

    /**
     * Constructor.
     * 
     * @param connectionStatistics
     *            The {@link ConnectionStatistics} to report statistics to
     */
    public ConnectionStatisticsDelegate(final IConnectionStatisticsCollector connectionStatistics) {
        this.connectionStatistics = connectionStatistics;
    }

    /**
     * Get the {@link ConnectionStatistics} for this connection.
     * 
     * @return The {@link ConnectionStatistics}
     */
    public IConnectionStatisticsCollector getConnectionStatistics() {
        return this.connectionStatistics;
    }

    /** {@inheritDoc} */
    @Override
    public void commit() throws SQLException {
        this.connectionStatistics.increaseCommits();

        super.commit();
    }

    /** {@inheritDoc} */
    @Override
    public Statement createStatement() throws SQLException {
        this.connectionStatistics.increaseStatementsCreated();

        return super.createStatement();
    }

    /** {@inheritDoc} */
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException {
        this.connectionStatistics.increaseStatementsCreated();

        return super.createStatement(resultSetType, resultSetConcurrency);
    }

    /** {@inheritDoc} */
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
        this.connectionStatistics.increaseStatementsCreated();

        return super.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
    }

    /** {@inheritDoc} */
    @Override
    public CallableStatement prepareCall(final String sql) throws SQLException {
        this.connectionStatistics.increaseCallableStatementsCreated();

        return super.prepareCall(sql);
    }

    /** {@inheritDoc} */
    @Override
    public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
        this.connectionStatistics.increaseCallableStatementsCreated();

        return super.prepareCall(sql, resultSetType, resultSetConcurrency);
    }

    /** {@inheritDoc} */
    @Override
    public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
        this.connectionStatistics.increaseCallableStatementsCreated();

        return super.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }

    /** {@inheritDoc} */
    @Override
    public PreparedStatement prepareStatement(final String sql) throws SQLException {
        this.connectionStatistics.increasePreparedStatementsCreated();

        return super.prepareStatement(sql);
    }

    /** {@inheritDoc} */
    @Override
    public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
        this.connectionStatistics.increasePreparedStatementsCreated();

        return super.prepareStatement(sql, autoGeneratedKeys);
    }

    /** {@inheritDoc} */
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
        this.connectionStatistics.increasePreparedStatementsCreated();

        return super.prepareStatement(sql, resultSetType, resultSetConcurrency);
    }

    /** {@inheritDoc} */
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
        this.connectionStatistics.increasePreparedStatementsCreated();

        return super.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }

    /** {@inheritDoc} */
    @Override
    public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException {
        this.connectionStatistics.increasePreparedStatementsCreated();

        return super.prepareStatement(sql, columnIndexes);
    }

    /** {@inheritDoc} */
    @Override
    public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException {
        this.connectionStatistics.increasePreparedStatementsCreated();

        return super.prepareStatement(sql, columnNames);
    }

    /** {@inheritDoc} */
    @Override
    public void rollback() throws SQLException {
        this.connectionStatistics.increaseRollbacks();

        super.rollback();
    }

    /** {@inheritDoc} */
    @Override
    public void rollback(final Savepoint savepoint) throws SQLException {
        this.connectionStatistics.increaseRollbacks();

        super.rollback(savepoint);
    }

    /** {@inheritDoc} */
    @Override
    public Savepoint setSavepoint() throws SQLException {
        this.connectionStatistics.incraseSavepoints();

        return super.setSavepoint();
    }

    /** {@inheritDoc} */
    @Override
    public Savepoint setSavepoint(final String name) throws SQLException {
        this.connectionStatistics.incraseSavepoints();

        return super.setSavepoint(name);
    }
}
